﻿@model cloudscribe.Core.Web.ViewModels.RoleAdmin.RoleListViewModel
@inject ICoreThemeHelper themeHelper
@inject IStringLocalizer<CloudscribeCore> sr
@inject IOptions<SiteConfigOptions> setupOptionsAccessor
@{
    var setupOptions = setupOptionsAccessor.Value;
    ViewData["BodyClass"] = "admin-section role-index";
    var themeSettings = themeHelper.GetThemeSettings();
    if (themeSettings.AdminSideNavExpanded) { ViewData["SideNavToggle"] = "show"; }
    ViewData["SideNavVisible"] = true;
}

<h2>@ViewData["Title"]</h2>
<p>
    <a class="btn btn-sm btn-primary" asp-action="RoleEdit" asp-controller="RoleAdmin" asp-route-siteId="@Model.SiteId">@sr["New Role"]</a>
</p>
<ul class=" list-group">
    @foreach (var item in Model.SiteRoles.Data)
    {
        <li class=" list-group-item d-flex flex-wrap">
            <h3 class="h4">
                @Html.DisplayFor(modelItem => item.RoleName)
                &nbsp;
            </h3>
            <div role="group">
                <span class="btn btn-outline-primary ">
                    <a asp-action="RoleMembers"
                       asp-controller="RoleAdmin"
                       asp-route-siteId="@item.SiteId"
                       asp-route-roleId="@item.Id">@sr["Members"]</a>
                    <span class="member-count">@Html.DisplayFor(modelItem => item.MemberCount)</span>
                </span>
                @if (item.IsDeletable(setupOptions.RolesThatCannotBeDeleted))
                {
                    <a class="btn btn-primary" asp-action="RoleEdit" asp-controller="RoleAdmin"
                       asp-route-siteId="@Model.SiteId"
                       asp-route-roleId="@item.Id">@sr["Edit"]</a>
                }
                @if (item.NormalizedRoleName != "ADMINISTRATORS")
                {
                    <button type="button" class="btn btn-secondary" 
                            data-bs-toggle="modal" 
                            data-bs-target="#copyRoleModal"
                            data-role-id="@item.Id"
                            data-role-name="@item.RoleName">
                        @sr["Copy"]
                    </button>
                }

            </div>
        </li>
    }
</ul>
<cs-pager asp-action="Index" asp-controller="RoleAdmin"
          asp-route-siteId="@Model.SiteId"
          asp-route-pageSize="@Model.SiteRoles.PageSize"
          cs-paging-pagesize="@Model.SiteRoles.PageSize"
          cs-paging-pagenumber="@Model.SiteRoles.PageNumber"
          cs-paging-totalitems="@Model.SiteRoles.TotalItems"
          cs-first-page-title='@sr["First Page"]'
          cs-last-page-title='@sr["Last Page"]'
          cs-previous-page-title='@sr["Previous page"]'
          cs-next-page-title='@sr["Next page"]'
          cs-pager-ul-class="pagination d-flex flex-wrap"
          cs-pager-li-current-class="page-item active"
          cs-pager-li-other-class="page-item"
          cs-pager-li-non-active-class="page-item disabled"
          cs-pager-link-current-class="page-link"
          cs-pager-link-other-class="page-link"></cs-pager>

<!-- Copy Role Modal -->
<div class="modal fade" id="copyRoleModal" tabindex="-1" aria-labelledby="copyRoleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form asp-action="CopyRole" asp-controller="RoleAdmin" method="post">
                @Html.AntiForgeryToken()
                <div class="modal-header">
                    <h5 class="modal-title" id="copyRoleModalLabel">@sr["Copy Role"]</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="sourceRoleId" name="SourceRoleId" />
                    <input type="hidden" id="sourceRoleName" name="SourceRoleName" />
                    <div class="mb-4">
                        <label for="newRoleName" class="form-label">@sr["New role name"]</label>
                        <input type="text" class="form-control" id="newRoleName" name="NewRoleName" required maxlength="50" minlength="3" />
                        <div class="form-text">
                            @sr["This will create a new role."]
                            @if (ViewData["HasDynamicPolicyHandlers"] as bool? == true)
                            {
                                @:@sr[" Any dynamic authorization policies that reference the source role will also reference the new role."]
                            }
                        </div>
                    </div>
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" id="includeExistingUsers" name="IncludeExistingUsers" value="true" />
                        <label class="form-check-label" for="includeExistingUsers">
                            @sr["Include existing users"]
                        </label>
                    </div>
                    <div class="form-text mb-3">@sr["If checked, any existing users in the source role will also become members of the new role."]</div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@sr["Cancel"]</button>
                    <button type="submit" class="btn btn-primary">@sr["Copy Role"]</button>
                </div>
            </form>
        </div>
    </div>
</div>

@section SideNav {
    <partial name="AdminSideNav" />
}
@section Toolbar{
    <partial name="AdminSideNavToggle" />
}
@section scripts {
    <partial name="AdminSideNavScripts" />
    <script src="~/cr/js/cloudscribe-copy-role-modal.js"></script>
}
